-- around_damage
-- create by shuzy
-- 道具： 对目标造成n点伤害，同时m%伤害溅射周围n范围内敌人

return {
    apply = function(source, target, classId, value)
        -- 传入攻击者信息
        local assailantInfo = {
            ["property_id"] = classId,
        };

        -- 道具伤害加成
        local damage = value[1];
        if PropertyM.query(classId, "target") ~= PROPERTY_SELF and value[4] > 0 then
            local prop = PropM.combine(source, "property_damage", 1);
            damage = PropM.apply(prop, damage);
        end

        -- 修正伤害值
        local fixDamage = PropertyM.fixPropertyDamage(source, target, classId, damage);

        -- 先对目标造成伤害
        if ItemM.query(classId, "apply") == "damage_and_drop" then
            -- 会掉落道具
            CombatM.doDamage(source, target, fixDamage, 0, true, { assailantInfo = assailantInfo, });
        else
            CombatM.doDamage(source, target, fixDamage, 0, false, { assailantInfo = assailantInfo, });
        end

        -- 溅射范围
        local ret = {};

        -- 遍历怪物周围所有已开且存活的怪
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end
        local coor = DungeonM.convertToCoor(pos);

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local c = DungeonM.convertToCoor(p);

            -- 有效范围内
            local near = math.abs(c.x - coor.x) <= value[2] and math.abs(c.y - coor.y) <= value[2];

            local grid = DungeonM.getGridByPos(p);

            -- 翻开且存活的怪物
            if  near and grid:isOpened() and
                grid:isMonster() and
                grid.monster ~= target and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        -- 溅射伤害
        damage = math.modf(damage * value[3] / 1000);

        for _, monster in pairs(ret) do
            -- 修正伤害值
            fixDamage = PropertyM.fixPropertyDamage(source, monster, classId, damage);
            -- 受创
            CombatM.doDamage(source, monster, fixDamage, 0, false, { assailantInfo = assailantInfo, ["delay_time"] = 0.65;});
        end

        -- 抛出使用道具攻击事件
        EventMgr.fire(event.PROPERTY_ATTACK, { ["classId"] = classId, ["target"] = target, ["source"] = source, ["moreTarget"] = ret});
    end,
};